package metu.tespit.session.course;

import java.io.File;
import java.io.RandomAccessFile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;

import metu.tespit.entitiy.accountManagement.User;
import metu.tespit.entitiy.accountManagement.UserRole;
import metu.tespit.entity.course.Announcement;
import metu.tespit.entity.course.LectureNote;
import metu.tespit.session.EntityManager.ExtendedEntityHome;
import metu.tespit.session.util.Events;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.async.Asynchronous;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.faces.Renderer;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

/**
 
 * @created Jun 9, 2012
 */
@Name("lectureNoteHome")
public class LectureNoteHome extends ExtendedEntityHome<LectureNote>{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7768656870164264708L;

	@RequestParameter
	protected Long lnId;

	@In(create=true)
	private Renderer renderer;

	private User toUser;

	@Override
	public Object getId()
	{
		if (lnId == null)
		{
			return super.getId();
		}
		else
		{
			return lnId;
		}
	}


	@In
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public void listener(UploadEvent event) throws Exception{

		UploadItem item = event.getUploadItem();
		this.instance.setContentType(item.getContentType());
		File file =item.getFile();
		RandomAccessFile f = new RandomAccessFile(file, "r");
		byte[] b = new byte[(int)f.length()];
		f.read(b);
		this.instance.setData(b);
		this.instance.setFileName(item.getFileName());

		
	}
	
	public String update(){
		String outcome = super.update();
		if(outcome.equalsIgnoreCase("updated")){
			this.raiseAsynchronousEvent(Events.LECTURE_NOTE_CREATED, this.instance);
		}
		return outcome;
	}
	
	@In(create=true)
	CourseHome courseHome;
	

	public String persistWithCourse(){

		if(courseHome!=null){
			this.instance.setCourse(courseHome.getInstance());
			this.instance.setDate(new Date());
			courseHome.getInstance().getLectureNotes().add(this.instance);
			String outcome = super.persist();
			if(outcome.equalsIgnoreCase("persisted")){
				this.raiseAsynchronousEvent(Events.LECTURE_NOTE_CREATED, this.instance);
			}
			return outcome;
		}
		else{
			return "FAIL";
		}
	}

	public void clearFileData(){
		
		this.instance.setData(null);
		this.instance.setFileName(null);
		this.instance.setContentType(null);
	}
	
	

    @Observer(Events.LECTURE_NOTE_CREATED)
    @Asynchronous
    public void sendMailToAllRegisteredStudents(LectureNote lectureNote) {
    	if(lectureNote == null){
    		return;
    	}
    	
        for (Iterator<User> iterator = lectureNote.getCourse().getStudents().iterator(); iterator.hasNext(); ) {
            try {
                toUser = iterator.next();
                renderer.render("/Mail/createLectureNote.xhtml");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

	public User getToUser() {
		return toUser;
	}

	public void setToUser(User toUser) {
		this.toUser = toUser;
	}

	public Long getLnId() {
		return (Long)getId();
	}

	public void setLnId(Long lnId) {
		this.setId(lnId);
	}
}

